package com.example.kbase.fragment.subFragment;


import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.kbase.R;
import com.example.kbase.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * homeFragment中的第二级Fragment
 * 获取对应目录下的文章题目list，本级获取数据库表的题目、作者信息。
 * bundle 传递数据
 */

public class MItem1Fragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private View view;
    private ListView listView;
    private ImageButton back;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mitem1,null);
        initView();
        return view;
    }

    private void initView() {
        listView = view.findViewById(R.id.mif1_lv);   //题目list
        back = view.findViewById(R.id.mit1_back_home);

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_list_item_1,getData());
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(this);
        back.setOnClickListener(this) ;
    }

    public List<String> getData() {
        List<String> data = new ArrayList<>();
        for(int i =0;i<5;i++){
            data.add(i+"article_name");
        }
        return data;
    }

    @Override
    public void onClick(View view) {
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setCustomAnimations(
                R.animator.fragment_slide_left_enter,
                R.animator.fragment_slide_left_exit,
                R.animator.fragment_slide_right_exit,
                R.animator.fragment_slide_right_enter).
                replace(R.id.content_layout,new HomeFragment()). //replace实现返回上一层效果
                addToBackStack(null); //  当前界面加入回退栈
        transaction.commit();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        FragmentManager manager= getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        MItem2Fragment mif2 = new MItem2Fragment();
//                TextView tv1 = (TextView) view;
//                Bundle bundle = new Bundle();
//                bundle.putCharSequence("1",tv1.getText().toString());
//                mif.setArguments(bundle);

        transaction.setCustomAnimations(
                R.animator.fragment_slide_left_enter,
                R.animator.fragment_slide_left_exit,
                R.animator.fragment_slide_right_exit,
                R.animator.fragment_slide_right_enter).
                replace(R.id.content_layout,mif2).
                addToBackStack(null);
        transaction.commit();
    }
}
